package com.android.demolayoutpages;







import java.util.Date;

import com.android.demolayoutpages.R.layout;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewDebug.FlagToString;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class MainActivity extends BaseActivity implements AnimationListener{

	private int height;
	private int width;
	private boolean FLAG;
	private View giftContentsView;
	private View categoryView;
	protected boolean menuOut;
	private AnimParams animParams;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView();
		setUpView();

	}

	private void setContentView() {

		LinearLayout layoutAppContents = (LinearLayout)findViewById(R.id.layout_app_contents);
		View contentsView = ((LayoutInflater)MainActivity.this.getSystemService(MainActivity.this.LAYOUT_INFLATER_SERVICE))
				.inflate(R.layout.layout_app_header, null);
		contentsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		layoutAppContents.addView(contentsView);



	}



	private void setUpView() {
		LinearLayout btnGifts = (LinearLayout)findViewById(R.id.btn_gifts);
	//	final LinearLayout layoutGifts = (LinearLayout)findViewById(R.id.layout_grid_gifts);
		Button btnFilter = (Button)findViewById(R.id.btn_left_nevigation);
		categoryView = (View)findViewById(R.id.layout_category);
		giftContentsView = (View)findViewById(R.id.layout_gift_contents);
		btnGiftsLayout.setOnClickListener(this);
		ListView lvCategory = (ListView)findViewById(R.id.listView_category);
		btnGifts.setClickable(false);




		GridView gvGifts=  (GridView)findViewById(R.id.gridView_gifts);

		gvGifts.setAdapter(new MenuImageAdapter(MainActivity.this));



		gvGifts.setOnItemClickListener(new  OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Toast.makeText(view.getContext(), "Position :"+position, Toast.LENGTH_SHORT).show();

			}
		});

		View headerView = ((LayoutInflater)MainActivity.this.getSystemService(MainActivity.this.LAYOUT_INFLATER_SERVICE))
				.inflate(R.layout.header_category_list, null, false);
		lvCategory.addHeaderView(headerView);
		lvCategory.setAdapter(new CategoryAdapter(MainActivity.this));
		btnFilter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//showToast(""+layoutGifts.getMeasuredWidth()+"x"+layoutGifts.getMeasuredHeight());

				animParams = new AnimParams();

				System.out.println("onClick " + new Date());
				MainActivity me = MainActivity.this;
				Context context = me;
				Animation anim;

				int w = giftContentsView.getMeasuredWidth();
				int h = giftContentsView.getMeasuredHeight();
				int left = (int) (giftContentsView.getMeasuredWidth() * 0.5);

				if (!menuOut) {
					// anim = AnimationUtils.loadAnimation(context, R.anim.push_right_out_80);
					anim = new TranslateAnimation(0, left, 0, 0);
					categoryView.setVisibility(View.VISIBLE);
					animParams.init(left, 0, left + w, h);
				} else {
					// anim = AnimationUtils.loadAnimation(context, R.anim.push_left_in_80);
					anim = new TranslateAnimation(0, -left, 0, 0);
					animParams.init(0, 0, w, h);
				}

				anim.setDuration(300);
				anim.setAnimationListener(me);
				//Tell the animation to stay as it ended (we are going to set the app.layout first than remove this property)
				anim.setFillAfter(true);


				// Only use fillEnabled and fillAfter if we don't call layout ourselves.
				// We need to do the layout ourselves and not use fillEnabled and fillAfter because when the anim is finished
				// although the View appears to have moved, it is actually just a drawing effect and the View hasn't moved.
				// Therefore clicking on the screen where the button appears does not work, but clicking where the View *was* does
				// work.
				// anim.setFillEnabled(true);
				// anim.setFillAfter(true);

				giftContentsView.startAnimation(anim);


			}
		});

	}

	public Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
		Bitmap result = null;
		try {
			result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(result);

			int color = 0xff424242;


			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
			RectF rectF = new RectF(rect);
			int roundPx = pixels;

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(color);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rect, rect, paint);
		} catch (NullPointerException e) {
			// return bitmap;
		} catch (OutOfMemoryError o) {
		}
		return result;
	}

	 



	@Override
	public void onAnimationEnd(Animation animation) {

		System.out.println("onAnimationEnd");
		// ViewUtils.printView("menu", menu);
		// ViewUtils.printView("app", app);
		menuOut = !menuOut;
		if (!menuOut) {
			categoryView.setVisibility(View.INVISIBLE);
		}
		layoutApp(menuOut);


	}

	private void layoutApp(boolean menuOut) {

		System.out.println("layout [" + animParams.left + "," + animParams.top + "," + animParams.right + ","
				+ animParams.bottom + "]");
		giftContentsView.layout(animParams.left, animParams.top, animParams.right, animParams.bottom);
		//Now that we've set the app.layout property we can clear the animation, flicker avoided :)
		giftContentsView.clearAnimation();



	}

	@Override
	public void onAnimationRepeat(Animation animation) {


	}

	@Override
	public void onAnimationStart(Animation animation) {


	}


	class AnimParams {
		int left, right, top, bottom;

		void init(int left, int top, int right, int bottom) {
			this.left = left;
			this.top = top;
			this.right = right;
			this.bottom = bottom;
		}
	}

	/*@Override
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
		case R.id.btn_gifts:

			break;

		default:
			break;
		}

	}*/
}
